﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadExpress.Models;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Repository
{
    public class BannerOptionManagementRepository : BaseManagementRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();
        public BannerOptionManagementModel GetModel(string pageType, string roleName, string keywords, string[] group, string[] position, int pageSize, int pageIndex, int id)
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.GroupList = new Repository.BannerGroupManagementRepository().GetAllItems();
            model.PositionList = new Repository.BannerPositionManagementRepository().GetAllItems();
            model.OptionList = GetAll(keywords, group, position, pageSize, pageIndex);

            if (id > 0)
            {
                model.BannerOptionInfo = GetSingle(id);
            }

            PaginateModel paging = new PaginateModel();
            int totalRecord = model.OptionList.Count;
            if (totalRecord > 0)
            {
                paging.PageIndex = pageIndex;
                paging.PageSize = pageSize;
                paging.TotalRecords = totalRecord;
                paging.TotalPages = (totalRecord % pageSize == 0) ? (totalRecord / pageSize) : (totalRecord / pageSize + 1);
            }
            else
            {
                paging.PageIndex = 1;
                paging.PageSize = 10;
                paging.TotalRecords = 0;
                paging.TotalPages = 1;
            }
            model.Paging = paging;

            return model;
        }

        public BannerOptionManagementModel GetModel(string pageType, string roleName, int pageSize, int pageIndex, int id)
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.GroupList = new Repository.BannerGroupManagementRepository().GetAllItems();
            model.PositionList = new Repository.BannerPositionManagementRepository().GetAllItems();
            model.OptionList = GetAll();

            if (id > 0)
            {
                model.BannerOptionInfo = GetSingle(id);
            }

            PaginateModel paging = new PaginateModel();
            int totalRecord = model.OptionList.Count;
            if (totalRecord > 0)
            {
                paging.PageIndex = pageIndex;
                paging.PageSize = pageSize;
                paging.TotalRecords = totalRecord;
                paging.TotalPages = (totalRecord % pageSize == 0) ? (totalRecord / pageSize) : (totalRecord / pageSize + 1);
            }
            else
            {
                paging.PageIndex = 1;
                paging.PageSize = 10;
                paging.TotalRecords = 0;
                paging.TotalPages = 1;
            }
            model.Paging = paging;

            return model;
        }

        public BannerOptionManagementModel GetModel(string pageType, string roleName, int id)
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.GroupList = new Repository.BannerGroupManagementRepository().GetAllItems();
            model.PositionList = new Repository.BannerPositionManagementRepository().GetAllItems();
            model.OptionList = GetAll();

            if (id > 0)
            {
                model.BannerOptionInfo = GetSingle(id);
            }

            return model;
        }

        public BannerOptionManagementModel GetModel(string pageType, string roleName)
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.GroupList = new Repository.BannerGroupManagementRepository().GetAllItems();
            model.PositionList = new Repository.BannerPositionManagementRepository().GetAllItems();

            return model;
        }

        public List<OptionModel> GetAllItems()
        {
            List<OptionModel> list = new List<OptionModel>();

            var items = (from c in entities.BannerOptions
                         orderby c.Name ascending
                         select c);

            list.Add(new OptionModel { ID = "-1", Name = "All" });
            // Add items
            foreach (var item in items)
            {
                OptionModel model = new OptionModel();
                model.ID = item.ID.ToString();
                model.Name = item.Name;
                list.Add(model);
            }

            return list;
        }

        public List<BannerOption> GetAll(string keywrods, string[] groupId, string[] positionId, int pageSize, int pageIndex)
        {
            List<int> arrGroup = new List<int>();
            List<int> arrPosition = new List<int>();

            // Query data
            var banners = (from c in entities.BannerOptions
                           select c);

            // Group list
            if (groupId.Length > 0 && int.Parse(groupId[0]) > 0)
            {
                for (int i = 0; i < groupId.Length; i++)
                {
                    arrGroup.Add(int.Parse(groupId[i]));
                }
                banners = banners.Where(c => arrGroup.Contains(c.GroupID.Value));
            }

            // Payment list
            if (positionId.Length > 0 && int.Parse(positionId[0]) > 0)
            {
                for (int i = 0; i < positionId.Length; i++)
                {
                    arrPosition.Add(int.Parse(positionId[i]));
                }
                banners = banners.Where(c => arrPosition.Contains(c.PositionID.Value));
            }

            // Keywords
            if (!string.IsNullOrEmpty(keywrods))
                banners = banners.Where(c => c.Name.Contains(keywrods) || c.ShortDesc.Contains(keywrods));

            return banners.OrderByDescending(c => c.ID).AsEnumerable().Skip((pageSize - 1) * pageIndex).Take(pageIndex).ToList();
        }

        public List<BannerOption> GetAll()
        {            
            return (from c in entities.BannerOptions
                         orderby c.ID descending
                         select c).ToList();
        }

        public BannerOption GetSingle(int id)
        {
            return (from _object in entities.BannerOptions where _object.ID == id select _object).FirstOrDefault();
        }

        public BannerOption GetSingle(string name)
        {
            return (from _object in entities.BannerOptions where _object.Name == name select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] BannerOption _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToBannerOptions(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.BannerOptions where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] BannerOption _object)
        {
            _object.EntityKey = (from BannerOption ac in entities.BannerOptions where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}